package com.xingdata.jjxc.activity;

import jar.tools.UdpTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.OBDEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.SystemInfo;

public class OBDActivity extends Base1Activity implements OnClickListener {
	private String TAG = "OBDActivity";
	private TextView title;
	private HashMap<String, ArrayList<OBDEntity>> hashMap = new HashMap<String, ArrayList<OBDEntity>>();
	private LinearLayout layout;
	private LayoutInflater inflater;
	private int inNormal,inWarn,inFault;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.test);
		doPsot_getObdParameter();

	}

	/**
	 * 获取OBD数据
	 */
	private void doPsot_getObdParameter() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());

		paramsMap.put("access_token", SystemInfo.getTokenEntity().getToken());
	
		if ( UdpTools.getHudCode()==null||UdpTools.getHudCode().length()==0) {
			paramsMap.put("hud_sn", SharedPreTools.getString(this, Common.HUD_SN, ""));
		}else {
			paramsMap.put("hud_sn", UdpTools.getHudCode());
		}
		HttpUtil.getInstance(this).Post(App.ZZD_REQUEST_FINDOBDCURRENT,
				paramsMap, new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						List<OBDEntity> obdEntities = JSON.parseArray(
								respEntity.getResult(), OBDEntity.class);
						for (OBDEntity entity : obdEntities) {
							String type = entity.getType();
							if (hashMap.containsKey(type)) {
								hashMap.get(type).add(entity);
							} else {
								ArrayList<OBDEntity> arrayList = new ArrayList<OBDEntity>();
								arrayList.add(entity);
								hashMap.put(type, arrayList);
							}

						}
						initData();
					}

					@Override
					public void onLoading(int vurrent) {

					}

					@Override
					public void onFailure(String result) {
						showToast(result);
					}
				});
	}

	private void initData() {
		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		layoutParams.setMargins(20, 12, 0, 12);
		for (Entry<String, ArrayList<OBDEntity>> entry : hashMap.entrySet()) {
			TextView typeTextView = new TextView(this);
			typeTextView.setText(entry.getKey());
			typeTextView.setLayoutParams(layoutParams);
			// 设置数据类型
			layout.addView(typeTextView);
			for (OBDEntity entity : entry.getValue()) {
				LinearLayout obd_item_layout = (LinearLayout) inflater.inflate(
						R.layout.obd_item, null);
				((TextView) obd_item_layout.findViewById(R.id.title))
						.setText(entity.getTitle());
				((TextView) obd_item_layout.findViewById(R.id.value))
						.setText(entity.getValue());
				ImageView flagButton = (ImageView)obd_item_layout. findViewById(R.id.flag);
				int flag = Integer.parseInt(entity.getFlag());
				switch (flag) {
				case 0:
					flagButton.setImageResource(R.drawable.duihao);
					inNormal++;
					break;
				case 1:
					flagButton.setImageResource(R.drawable.tanhao);
					inWarn++;
					break;
				case 2:
					flagButton.setBackgroundResource(R.drawable.cuowu);
					inFault++;
					break;
				default:
					break;
				}
				layout.addView(obd_item_layout);
			}

			((TextView)findViewById(R.id.inNormal)).setText(inNormal+"");
			((TextView)findViewById(R.id.inWarn)).setText(inWarn+"");
			((TextView)findViewById(R.id.inFault)).setText(inFault+"");
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_obd;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "OBD参数";
	}

	@Override
	protected void initViews() {
		// title = (TextView) findViewById(R.id.title);
		// title.setText("OBD");
		// ((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		layout = (LinearLayout) findViewById(R.id.my_layout);

		inflater = LayoutInflater.from(this);

		// LinearLayout itemLayout1 = (LinearLayout) inflater.inflate(
		// R.layout.obd_item, null);
		// layout.addView(itemLayout1);
		//
		// LinearLayout itemLayout2 = (LinearLayout) inflater.inflate(
		// R.layout.obd_item, null);
		// layout.addView(itemLayout2);
		//
		// LinearLayout itemLayout3 = (LinearLayout) inflater.inflate(
		// R.layout.obd_item, null);
		// layout.addView(itemLayout3);

	}

}
